package mobi.bantu.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import mobi.bantu.bean.Page;
import mobi.bantu.bean.SearchParam;
import mobi.bantu.bean.task.Report;
import mobi.bantu.config.SysConf;
import mobi.bantu.service.TaskService;
import mobi.bantu.utils.DateUtils;
import mobi.bantu.utils.ExportUtil;

@Controller
@RequestMapping("task")
public class TaskController {

    @Resource
    private TaskService taskService;

    @RequestMapping("list")
    public String list() {
        return "task/list";
    }

    @RequestMapping("list-data")
    @ResponseBody
    public Page<Report> listData(SearchParam sp) {
        return Page.create(taskService.list(sp.getSp()), taskService.count(sp.getSp()));
    }


    @RequestMapping("download")
    public void download(SearchParam sp, HttpServletResponse response) {
        Map<String, Object> param = sp.getSpDown();
        List<Report> rs = taskService.list(param);
        if (null == rs || rs.isEmpty()) {
            return;
        }
        String fileName = "xyBaoXiu_" + DateUtils.formatDate(new Date()) + ".xls";
        ExportUtil.exportSimpExcel(response, fileName, rs, Report.class, "项目信息");
    }


    @RequestMapping("detail")
    public String detail(@RequestParam(required = true) Long id, Model model) {
        Report report = taskService.getById(id);
        model.addAttribute("imgPath", SysConf.get("img.path"));
        model.addAttribute("info", report);
        return "task/detail";
    }

}
